package com.example.teaching_server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;

/**
 * 通知接收者实体类
 *
 * @author Teaching Server
 * @since 1.0.0
 */
@Getter
@Setter
@ToString(exclude = "notice")
@EqualsAndHashCode(of = "recipientId")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "notice_recipients", indexes = {
        @Index(name = "idx_recipient_notice", columnList = "notice_id"),
        @Index(name = "idx_recipient_type", columnList = "recipient_type,target_id"),
        @Index(name = "idx_recipient_read", columnList = "read_status")
})
public class NoticeRecipientEntity {

    /**
     * 接收者ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "recipient_id")
    private Long recipientId;

    /**
     * 通知ID
     */
    @NotNull(message = "通知ID不能为空")
    @Column(name = "notice_id", nullable = false)
    private Long noticeId;

    /**
     * 接收者类型
     */
    @NotBlank(message = "接收者类型不能为空")
    @Size(max = 20, message = "接收者类型长度不能超过20个字符")
    @Column(name = "recipient_type", nullable = false, length = 20)
    private String recipientType;

    /**
     * 目标ID
     */
    @Column(name = "target_id")
    private Long targetId;

    /**
     * 阅读状态
     */
    @NotNull(message = "阅读状态不能为空")
    @Column(name = "read_status", nullable = false)
    @Builder.Default
    private Boolean readStatus = false;

    /**
     * 阅读时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "read_at")
    private LocalDateTime readAt;

    /**
     * 创建时间
     */
    @CreationTimestamp
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    /**
     * 关联通知实体
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "notice_id", insertable = false, updatable = false)
    private NoticeEntity notice;
}
